<template>
  <div>
    <a-form-model ref="form" :model="model" style="max-width: 500px; margin: 40px auto 0;" :rules="validatorRules">
      <a-form-model-item label="排班时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-date-picker v-model="model.rangeDateTime" format="YYYY-MM-DD"  valueFormat='YYYY-MM-DD'  :disabled-date="disabledStartDate"/>
      </a-form-model-item>
      <a-form-model-item label="排班科室" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departId">
        <a-select v-model="model.departId" placeholder="请选择排班的科室">
          <a-select-option v-for="item in departOption" :key="item.key" :value="item.value">{{ item.title }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="排班方案" :labelCol="labelCol" :wrapperCol="wrapperCol" required prop="planId">
        <a-select v-model="model.planId" placeholder="请选择班次方案">
          <a-select-option v-for="item in planOption" :key="item.key" :value="item.key">{{ item.label }}
          </a-select-option>
        </a-select>
        <div class='ant-form-explain'>注：如职工排班生成，就会跳过生成该排班表</div>
      </a-form-model-item>
      <a-form-item :wrapperCol="{span: 19, offset: 5}">
        <a-button type="primary" @click="nextStep">下一步</a-button>
      </a-form-item>
    </a-form-model>


  </div>
</template>

<script>
import CommonRuleParams from '@views/his/scheduling/RuleForm/CommonSchedulingRuleParams'
import { httpAction, getAction, postAction } from '@/api/manage'
import pick from 'lodash.pick'
import { queryMyDepartTreeList } from '@api/api'
import moment from 'moment'

export default {
  name: 'RuleStep1',
  components: {
    CommonRuleParams
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {
        numConfig:"0",
        timeId:"",
      },
      rangeDateTime: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      isShowTimeChoose:false,
      timeOption: [],
      planOption: [],
      departOption: [],
      registeredfeeOptions: [],
      validatorRules: {
        name: [{ required: true, message: '请填写此次排班名称!' }],
        planId: [{ required: true, message: '请选择班次方案!' }]
      },
    }
  },
  created() {
    console.log(' -- RuleStep1 created -- ')
    this.initRequestData()
    this.$nextTick(() => {
      Object.assign(this.model, CommonRuleParams.commonRuleForm1Data.dataSource)
      this.form.setFieldsValue(pick(this.model, 'storeInfoId'))
    })
  },
  methods: {
    moment,
    //只能选择当天日期后的时间
    disabledStartDate(current) {
      return current && current < moment().subtract(1, 'days')
    },
    onChange (selectedValue) {
      if(selectedValue=="1"){
         this.isShowTimeChoose=true;
      }else{
         this.isShowTimeChoose=false;
      }
    },
    initRequestData() {
      let that = this
      queryMyDepartTreeList().then((res) => {
          if (res.success && res.result) {
            that.departOption = res.result
          }
        }
      ),
      // getAction('his/registeredfee/all').then((res) => {
      //   that.registeredfeeOptions = res.result
      // }),
      getAction('/his/scheduling/plan/selectList').then((res) => {
        that.planOption = res.result
      }),
      getAction('/his/time/config/selectList').then((res) => {
        that.timeOption = res.result
      })
    },
    nextStep() {
      let that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          let formData = Object.assign(this.model, values)
          console.log('commonRuleForm1Data formData >>>', formData)
          this.$confirm({
            title: "确认提交",
            content: "是否确认生成排班表?",
            onOk: function () {
              postAction('/his/scheduling/allowAdd',{"departId":that.model.departId,"startDate":that.model.rangeDateTime,"planId":that.model.planId}).then((res) => {
                if(res.success){
                  formData.schedulingCode=res.result;
                  Object.assign(CommonRuleParams.commonRuleForm1Data.dataSource, formData)
                  that.$emit('nextStep')
                }else{
                  that.$message.warn(res.message)
                }
              })
            }
          });
        }
      })
    }

  }
}
</script>

<style lang="less" scoped>
.stepFormText {
  margin-bottom: 24px;

  .ant-form-item-label,
  .ant-form-item-control {
    line-height: 22px;
  }
}

</style>